from flask import current_app
from flask import g, jsonify
from flask import render_template
from flask import request

from info import db
from info.Modules.profile import profile_blu
from info.constants import QINIU_DOMIN_PREFIX
from info.libs.qiuniu_image_storage import storage
from info.utils.common_grep import user_login_data
from info.utils.response_code import RET



#个人信息设置接口
@profile_blu.route('/base_info',methods =['POST' ,'GET'])
@user_login_data
def user_base_info():
    user =g.user
    if request.method == 'POST' :
        params_dict = request.json
        nick_name = params_dict.get('nick_name')
        signature = params_dict.get('signature')
        gender = params_dict.get('gender')

        if not all([nick_name, signature,gender]):
            return jsonify(errno=RET.PARAMERR, errmsg="没昵称")
        if gender not in ['MAN' ,'WOMAN']:
            return jsonify(errno=RET.PARAMERR, errmsg="gender参数错误")

        user.nick_name = nick_name
        user.signature = signature
        user.gender = gender

        try:
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="数据库保存失败")

        return jsonify(errno=RET.OK, errmsg="成功")
    else:
        data ={ "user_info": user.to_dict()}
        return render_template('news/user_base_info.html',data =data)

@profile_blu.route('/info')
@user_login_data
def user_info():
    user =g.user

    data ={ "user_info": g.user.to_dict()}
    return render_template('news/user.html', data= data)

#个人头像设置接口
@profile_blu.route('/pic_info',methods =['POST' ,'GET'])
@user_login_data
def user_pic_info():
    user =g.user
    if request.method == 'POST' :
        avatar = request.files.get('avatar')
        if not avatar:
            return jsonify(errno=RET.PARAMERR, errmsg="不再")
        try:
            key =storage(avatar.read())
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="第三方错误")
        try:
            user.avatar_url = key
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="数据库保存错误")
        return jsonify(errno=RET.OK, errmsg="成功",avatar_url =QINIU_DOMIN_PREFIX +key)
    else:
        data = {"user_info": g.user.to_dict()}
        return render_template('news/user_pic_info.html', data=data)


#修改密码设置接口
@profile_blu.route('/pass_info',methods =['POST' ,'GET'])
@user_login_data
def user_pass_info():
    user =g.user
    if request.method == 'POST' :
        params_dict = request.json
        old_password = params_dict.get('old_password')
        new_password = params_dict.get('new_password')
        if not all([old_password, new_password]):
            return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
        if not user.check_passowrd(old_password):
            return jsonify(errno=RET.PARAMERR, errmsg="密码错误")
        if old_password == new_password:
            return jsonify(errno=RET.PARAMERR, errmsg="密码一样")
        try:
            user.password = new_password
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="数据库保存错误")
        return jsonify(errno=RET.OK, errmsg="成功")
    else:
        data = {"user_info": g.user.to_dict()}
        return render_template('news/user_pass_info.html', data=data)
